using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;

public partial class admin_news_add : SiteAdminBasePage
{
    ReadWriteCustomCollection<News> _related;

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!Page.IsPostBack)
        {
            LoadData();
            BindData();
            InitControls();
        }

    }
    protected void Create_Click(object sender, ImageClickEventArgs e)
    {
        try
        {         
            if (Page.IsValid)
            {
                DateTime newsDate;
                DateTime.TryParse(tbxNewsDate.Text, out newsDate);
                News news = new News(tbxNewsName.Text, fckNewsBriefDescription.Value, fckNewsDescription.Value, "", newsDate, tbxDisplayOrder.Text, cbxIsActive.Checked, false);
                news.Create();
                LoadData();
                foreach (News tmpNews in _related)
                {
                    news.AddRelatedNews(tmpNews);
                }
                uccResponseMessage.Visible = true;
                uccResponseMessage.SetSuccessMessage("New News Created.");
                
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to create the News");
            uccResponseMessage.Visible = true;
        }
    }

    private void BindData()
    {
        grvRelated.DataSource = _related;
        grvRelated.DataBind();

        ReadWriteCustomCollection<News> news = News.GetNews(99999);
        ddlRelatedList.DataSource = news;
        ddlRelatedList.DataTextField = "NewsName";
        ddlRelatedList.DataValueField = "NewsId";
        ddlRelatedList.DataBind();
    }

    private void LoadData()
    {
        _related = new ReadWriteCustomCollection<News>();
        foreach (GridViewRow row in grvRelated.Rows)
        {
            HiddenField hdfRelatedId = (HiddenField)row.FindControl("hdfRelatedId");
            int relatedId = int.Parse(hdfRelatedId.Value);
            _related.Add(News.Load(relatedId));
        }
    }

    private void InitControls()
    {
        imbRemove.Attributes.Add("onclick", "javascript:return confirm('Are you sure that you want to delete these items?')");
    }

    protected void Add_Related_Items(object sender, EventArgs e)
    {
        try
        {
            int relatedNewsId = 0;
            int.TryParse(ddlRelatedList.SelectedItem.Value, out relatedNewsId);
            LoadData();
            for (int i = 0; i < _related.Count; i++)
            {
                if (_related[i].NewsId == relatedNewsId)
                {
                    return;
                }
            }
            _related.Add(News.Load(relatedNewsId));
            BindData();
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to Add Related News");
            uccResponseMessage.Visible = true;
        }
    }
    protected void Delete_Related_Items(object sender, ImageClickEventArgs e)
    {
        try
        {
            LoadData();
            foreach (GridViewRow row in grvRelated.Rows)
            {
                CheckBox cbxRelated = (CheckBox)row.FindControl("cbxSelect");
                if (cbxRelated.Checked)
                {
                    HiddenField hdfRelatedId = (HiddenField)row.FindControl("hdfRelatedId");
                    int relatedNewsId = 0;
                    if (int.TryParse(hdfRelatedId.Value, out relatedNewsId))
                    {
                        News temp = News.Load(relatedNewsId);
                        int index = 0;
                        for (int i = 0; i < _related.Count; i++)
                        {
                            if (_related[i].NewsId == temp.NewsId)
                            {
                                index = i;
                                break;
                            }
                        }
                        _related.RemoveAt(index);
                    }
                }
            }
            BindData();
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to Add Related News");
            uccResponseMessage.Visible = true;
        }
    }
    protected void Related_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            CheckBox cbxSelectAll = (CheckBox)e.Row.FindControl("cbxSelectAll");
            cbxSelectAll.Attributes.Add("onclick", "javascript:SelectAll('" + grvRelated.ClientID + "','" + cbxSelectAll.ClientID + "','cbxSelect')");
        }
    }

    private void ClearControls()
    {
        tbxNewsName.Text = "";
        fckNewsBriefDescription.Value = "";
        fckNewsDescription.Value = "";
        tbxNewsDate.Text = "";
        tbxDisplayOrder.Text = "";
        cbxIsActive.Checked = false;
    }
}

